<template>
  <base-layout>
    <template #searchPanel>
      <search-panel
        v-slot="{ form }"
        :selectPer="'purchaseWh:page'"
        @search="search"
        :showSearchButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.status"
          placeholder="退货状态"
        >
          <el-option
            v-for="item in getDict(['采购退货状态'])['采购退货状态']"
            :key="item.index"
            :value="item.value"
            :label="item.text"
          />
        </el-select>
        <CarModel
          style="width: 350px"
          v-model="modelList"
          ref="carModel"
        />
        <el-date-picker
          v-model="form.inTime"
          type="datetimerange"
          start-placeholder="退货时间"
          end-placeholder="退货时间"
          clearable
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
        />
        <el-input
          v-model="form.purchaseReturnNum"
          clearable
          placeholder="请输入退货编号"
          style="width: 200px"
        />
        <el-input
          v-model="form.vin"
          clearable
          placeholder="请输入vin码"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @row-dblclick="handleDetails"
        :width="160"
      >
        <template #action="{ row }">
          <i
            v-if="row.returnStatus==='待审批'"
            class='el-icon-s-custom'
            title='销售经理审核'
            style='margin-left: 15px; cursor: pointer'
            v-permission="'purchaseWh:saleReturn'"
            @click='pendingApproval(row)'
          ></i>
          <i
            v-if="row.returnStatus==='流转中'"
            class='el-icon-s-custom'
            title='财务经理审核'
            style='margin-left: 15px; cursor: pointer'

            @click='pendingApproval(row)'
          ></i>
          <i
            v-if="row.returnStatus==='审批通过'"
            class='el-icon-right'
            title='确认退货'
            style='margin-left: 15px; cursor: pointer'
            v-permission="'purchaseWh:saleReturn'"
            @click='handleRebate(row)'
          ></i>
          <i
            v-if="row.returnStatus==='待审批'"
            class='el-icon-refresh-left'
            title='撤销'
            style='margin-left: 15px; cursor: pointer'
            v-permission="'purchaseReturn:revocation'"
            @click='handleReport(row)'
          ></i>
        </template>
      </data-table>
      <moduleDialog v-if="detailData" :visible="visible" @set-visible="setVisible" :detailData="detailData"/>
      <el-dialog
        title="审核"
        :visible.sync="visibleShow"
        width="25%"
        :before-close="handleClose">
        <el-form ref="ruleform" :model="ruleForm" label-width="80px">
          <el-form-item label="审核状态">
            <el-radio-group v-model="ruleForm.status">
              <el-radio label="0">通过</el-radio>
              <el-radio label="1">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea" placeholder='请输入' :autosize="{ minRows: 4, maxRows: 4}" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handeleOk">确 定</el-button>
        </span>
      </el-dialog>
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import { CarModel } from '@/components/index'
import moduleDialog from './components/moduleDialog'
import {
  getList,
  deleteById,
  revoke, inventoryOkOutBound
} from '@/api/pre-sale/newCarStock/purchaseReturn'
import {
  appval
} from '@/api/pre-sale/newCarStock/salePutOut'
import { getDict } from '@/utils/index'
import { saleReturn } from '@/api/pre-sale/storageManagement'

export default {
  components: {
    CarModel,
    moduleDialog
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: getList,
      deleteData: deleteById,
      dataName: ['content', 'totalRecords'],
      format(res) {
        return res.map((data) => {
          data.brandSeriesModel = data.brandName + '/' + data.seriesName + '/' + data.modelName
          data.nameAndPhone = data.returnUserName + '/' + data.returnPhone
          data.storageNames = data.storageName + '/' + data.storageSiteName
          return data
        })
      }
    })
  ],
  data() {
    return {
      searchOptions: { purchaseType: '采购退货出库' },
      visibleShow: false,
      ruleForm: {
        status: '0',
        remark: ''
      },
      detailData: null,
      detailId: '',
      visible: false,
      tableDataTwo: [],
      modelList: [],
      getDict: getDict,
      form: {
        vin:'',
        purchaseReturnNum: '',
        brandId: '',
        seriesId: '',
        modelId: ''
      },
      tableColumnAttributes: [
        {
          name: '门店',
          value: 'storeName',
          type: 'text'
        },
        {
          name: '退货编号',
          value: 'purchaseReturnNum',
          type: 'text'
        },
        {
          name: '入库编号',
          value: 'purchaseWhNum',
          type: 'text'
        },
        {
          name: '配车合同号',
          value: 'contractNum',
          type: 'text'
        },
        {
          name: '品牌/车系/车型',
          value: 'brandSeriesModel',
          type: 'text'
        },
        {
          name: '车架号',
          value: 'vin',
          type: 'text'
        },
        {
          name: '退货仓库/仓位',
          value: 'storageNames',
          type: 'text'
        },
        {
          name: '退货类型',
          value: 'storageNames',
          type: 'text'
        },
        {
          name: '退货供应商',
          value: 'supplierName',
          type: 'text'
        },
        {
          name: '退货负责人/电话',
          value: 'nameAndPhone',
          type: 'text'
        },
        {
          name: '实际进价',
          value: 'realityPrice',
          type: 'dict',
          dict: '订单类别'
        },
        {
          name: '退货原因',
          value: 'returnRemark',
          type: 'text'
        },
        {
          name: '状态',
          value: 'returnStatus',
          type: 'text'
        },
        {
          name: '退货时间',
          value: 'returnTime',
          type: 'text'
        }
      ],
      sourceCode: '',
      dict: {}
    }
  },
  methods: {
    handleRebate({ id }) {
      this.$confirm('是否确认退货?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await inventoryOkOutBound(id)
        this.setTableData()
        this.$message.success('操作成功')
      })
    },
    // 审批取消
    handleClose() {
      this.visibleShow = false
    },
    // 审批确认
    async handeleOk(row) {
      await appval({
        sourceCode: this.sourceCode,
        ...this.ruleForm
      }).then(res => {
        const { success } = res
        if (success) {
          this.visibleShow = false
          this.setTableData()
        }
      })
    },
    // 销售经理审批
    pendingApproval(row) {
      this.visibleShow = true
      this.sourceCode = row.purchaseReturnNum
      this.ruleForm = {
        status: '0',
        remark: ''
      }
    },
    // 撤销
    async  handleReport(row) {
      await revoke(row.id).then(res => {
        this.setTableData()
      })
    },
    // 查看
    handleDetails(row) {
      this.visible = true
      this.detailData = row
    },
    setVisible(val) {
      this.visible = val
      this.detailData = null
    },
    // 查询
    search(form) {
      if (this.modelList.length > 0) {
        form.brandId = this.modelList[0]
        form.seriesId = this.modelList[1]
        form.modelId = this.modelList[3]
      } else {
        form.brandId = ''
        form.seriesId = ''
        form.modelId = ''
      }
      if (form.inTime && form.inTime.length > 0) {
        form.minTime = form.inTime[0]
        form.maxTime = form.inTime[1]
      } else {
        form.minTime = ''
        form.maxTime = ''
      }
      delete form.inTime
      this.searchOptions = { ...this.searchOptions, ...form }
      this.currentPage = 1
      this.setTableData()
    }
  }
}
</script>
